﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI.WebControls;

namespace eBusiness.Areas.Admin.Controllers
{
    [Authorize]
    public class UsersController : Controller
    {
        //
        // GET: /Admin/Users/

        public ActionResult Index(int pageNumber = 1, int pageSize = 10)
        {
            var numberOfRecord = 0;
            //List<MembershipUser> allusers = Membership.GetAllUsers().Cast<MembershipUser>().ToList();

            //if (username != null)
            //{
            //    numberOfRecord = (from user in allusers where user.UserName.Contains(username) select user).Count();
            //}
            //else
            //{
            //    numberOfRecord = allusers.Count();
            //}
            var users = Membership.GetAllUsers(pageNumber-1, pageSize, out numberOfRecord).Cast<MembershipUser>().ToList();

            long numberOfPage = numberOfRecord % pageSize == 0
                                   ? numberOfRecord / pageSize
                                   : numberOfRecord / pageSize + 1;

            ViewBag.NumberOfPage = numberOfPage;
            ViewBag.CurrentPage = pageNumber;

            

            return View(users);
        }


        public ActionResult Unlock(Guid userId)
        {
            try
            {
                MembershipUser currentUser = Membership.GetUser(userId, true /* userIsOnline */);
                var unlockSuccessful = currentUser.UnlockUser();
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return View("Error");
            }
        }

        public ActionResult ApprovalUser(Guid userId)
        {
            try
            {
                MembershipUser currentUser = Membership.GetUser(userId, true /* userIsOnline */);
                if (currentUser != null)
                {
                    currentUser.IsApproved = !currentUser.IsApproved;
                    Membership.UpdateUser(currentUser);
                }
                return RedirectToAction("Index");
            }
            catch (Exception)
            {
                return View("Error");
            }
        }
    }
}
